package com.ruoyi.carShop.controller;

import com.ruoyi.carShop.domain.Statistics;
import com.ruoyi.carShop.domain.Withdrawal;
import com.ruoyi.carShop.service.BusinessStatisticsService;
import com.ruoyi.carShop.service.LoadService;
import com.ruoyi.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/shopStatistics")
public class BusinessStatisticsController {

    @Autowired
    private BusinessStatisticsService service;

    @Autowired
    private LoadService loadService;

    @GetMapping("/getCount")
    public R<Map<String, Object>> getCount(Statistics statistics){
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)attributes;
        HttpServletRequest request = sra.getRequest();
        //2 从请求头获取token
        String token = request.getHeader("token");

        statistics.setSeller((long) loadService.getToken(token));
        System.out.println(statistics);
        Map<String, Object> map = new HashMap<>();
        // 所有订单数据
        map.put("all", service.getCount(statistics));
        // 订单类型分类的数据
        map.put("type", service.getCountByType(statistics));
        return R.ok(map);
    }

    /**
     * 获取商家当前账户余额，累计收入、累计提现、收支明细数据（订单收入，提现列表）
     * @return Map
     */
    @GetMapping("/getBalance")
    private R<Map<String, Object>> getBalance(){
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)attributes;
        HttpServletRequest request = sra.getRequest();
        //2 从请求头获取token
        String token = request.getHeader("token");

        Long shopId = (long) loadService.getToken(token);

        Map<String, Object> map = new HashMap<>();
        // 账户余额
        BigDecimal balance = service.getShopBalance(shopId);
        map.put("balance", balance != null ? balance : 0);
        // 累计收入
        BigDecimal income = service.getShopOrderIncome(shopId);
        map.put("income", income != null ? income : 0);
        // 累计提现
        BigDecimal withdrawal = service.getShopWithdrawal(shopId);
        map.put("withdrawal", withdrawal != null ? withdrawal : 0);
        // 明细数据
        map.put("orderList", service.getTheShopOrder(shopId));
        map.put("withdrawalList", service.getWithdrawalList(shopId));

        return R.ok(map);
    }

    // 提现数据
    @RequestMapping("/getWithdrawalList")
    private R<List<Withdrawal>> getWithdrawalList(){
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)attributes;
        HttpServletRequest request = sra.getRequest();
        //2 从请求头获取token
        String token = request.getHeader("token");

        return R.ok(service.getWithdrawalList((long) loadService.getToken(token)));
    }

    // 提现申请
    @PostMapping("/addWithdrawal")
    private R addWithdrawal(@RequestBody Withdrawal withdrawal){
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)attributes;
        HttpServletRequest request = sra.getRequest();
        //2 从请求头获取token
        String token = request.getHeader("token");

        withdrawal.setMerchantId((long) loadService.getToken(token));
        System.out.println(withdrawal);
        return R.ok(service.addWithdrawal(withdrawal));
    }

}
